package site.assad.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import site.assad.domain.User;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * Author: Al-assad 余林颖
 * E-mail: yulinying_1994@outlook.com
 * Date: 2017/12/17 12:52
 * Description:
 */
@Repository
public class UserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void addUser(final User user){
        final String sql = "INSERT INTO t_user(user_name,user_password,user_email,user_birthday) " +
                "VALUES(?,?,?,?)";
        jdbcTemplate.update(sql,new Object[]{user.getUserName(),user.getPassword(),user.getEmail(),user.getBirthday()});
    }

    public User getUserById(final String userId){
        final String sql = "SELECT user_id,user_name,user_password,user_email,user_birthday " +
                "FROM t_user WHERE user_id = ?";
        User user = new User();
        jdbcTemplate.query(sql, new Object[]{userId}, new RowCallbackHandler() {
            @Override
            public void processRow(ResultSet rs) throws SQLException {
                user.setUserId(rs.getInt("user_id"));
                user.setUserName(rs.getString("user_name"));
                user.setPassword(rs.getString("user_password"));
                user.setEmail(rs.getString("user_email"));
                user.setBirthday(rs.getDate("user_birthday"));
            }
        });
        return user;
    }

    public User findUserByName(final String userName){
        final String sql = "SELECT user_id,user_name,user_password,user_email,user_birthday " +
                "FROM t_user WHERE user_name = ?";
        User user = new User();
        jdbcTemplate.query(sql, new Object[]{userName}, new RowCallbackHandler() {
            @Override
            public void processRow(ResultSet rs) throws SQLException {
                user.setUserId(rs.getInt("user_id"));
                user.setUserName(rs.getString("user_name"));
                user.setPassword(rs.getString("user_password"));
                user.setEmail(rs.getString("user_email"));
                user.setBirthday(rs.getDate("user_birthday"));
            }
        });
        return user;
    }

    public List<User> getAllUser(){
        final String sql = "SELECT user_id,user_name,user_password,user_email,user_birthday " +
                "FROM t_user ";
        List<User> userList = (List<User>)jdbcTemplate.query(sql, new RowMapper<User>() {
            @Override
            public User mapRow(ResultSet rs, int rowNum) throws SQLException {
                User user = new User();
                user.setUserId(rs.getInt("user_id"));
                user.setUserName(rs.getString("user_name"));
                user.setPassword(rs.getString("user_password"));
                user.setEmail(rs.getString("user_email"));
                user.setBirthday(rs.getDate("user_birthday"));
                return user;
            }
        });
        return userList;
    }



}
